<?php

/**
 * This is the model class for table "{{opus}}".
 *
 * The followings are the available columns in table '{{opus}}':
 * @property integer $id
 * @property string $opus_title
 * @property string $opus_desc
 * @property integer $opus_type
 * @property integer $st_id
 * @property string $opus_from
 * @property string $opus_source_type
 * @property integer $total_zan
 * @property string $create_time
 */
class Opus extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{opus}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('opus_type, st_id, total_zan', 'numerical', 'integerOnly'=>true),
			array('opus_title', 'length', 'max'=>50),
			array('opus_desc', 'length', 'max'=>255),
			array('opus_from, opus_source_type', 'length', 'max'=>10),
			array('create_time', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, opus_title, opus_desc, opus_type, st_id, opus_from, opus_source_type, total_zan, create_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'st'=>array(self::BELONGS_TO, 'People', 'st_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'opus_title' => 'Opus Title',
			'opus_desc' => 'Opus Desc',
			'opus_type' => 'Opus Type',
			'st_id' => 'St',
			'opus_from' => 'Opus From',
			'opus_source_type' => 'Opus Source Type',
			'total_zan' => 'Total Zan',
			'create_time' => 'Create Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('opus_title',$this->opus_title,true);
		$criteria->compare('opus_desc',$this->opus_desc,true);
		$criteria->compare('opus_type',$this->opus_type);
		$criteria->compare('st_id',$this->st_id);
		$criteria->compare('opus_from',$this->opus_from,true);
		$criteria->compare('opus_source_type',$this->opus_source_type,true);
		$criteria->compare('total_zan',$this->total_zan);
		$criteria->compare('create_time',$this->create_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Opus the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * 
	 * @param unknown $type 1:我的作品,2:其他作品,3:课堂资源
	 * @param unknown $user_info
	 */
	public function getOpusList($type,$user_info){
	    $db = Yii::app()->db->createCommand();
	    if($type==1){//�༶����
	        $cid = $user_info->cid;
	    
	        $haipan_sql = "SELECT M.pm
	        FROM (SELECT A.*,@rank:=@rank+1 as pm
	        FROM ( SELECT id,point FROM
	        {{people}} WHERE cid='$cid' AND role=1 ORDER BY point DESC ) A ,(SELECT @rank:=0) B ) M
	        WHERE M.`id`='$user_info->id' ORDER BY M.pm ASC";
	        $connection = Yii::app()->db;
	        $command = $connection->createCommand($haipan_sql);
	        $result = $command->queryRow();
	        $paihang = $result['pm'];
	    }elseif($type==2){
	        $gid = $user_info->gid;
	        $haipan_sql = "SELECT M.pm
	        FROM (SELECT A.*,@rank:=@rank+1 as pm
	        FROM ( SELECT id,point FROM
	        {{people}} WHERE gid='$gid' AND role=1 ORDER BY point DESC ) A ,(SELECT @rank:=0) B ) M
	        WHERE M.`id`='$user_info->id' ORDER BY M.pm ASC";
	        $connection = Yii::app()->db;
	        $command = $connection->createCommand($haipan_sql);
	        $result = $command->queryRow();
	        $paihang = $result['pm'];
	    }elseif($type==3){
	        $gid = $user_info->gid;
	        $haipan_sql = "SELECT M.pm
	        FROM (SELECT A.*,@rank:=@rank+1 as pm
	        FROM ( SELECT id,point FROM
	        {{people}} WHERE gid='$gid' AND role=1 ORDER BY point DESC ) A ,(SELECT @rank:=0) B ) M
	        WHERE M.`id`='$user_info->id' ORDER BY M.pm ASC";
	        $connection = Yii::app()->db;
	        $command = $connection->createCommand($haipan_sql);
	        $result = $command->queryRow();
	        $paihang = $result['pm'];
	    }
	}
}
